package controllers;

import java.util.Collection;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import services.CommentService;
import domain.Comment;

@Controller
@RequestMapping("/comment")
public class CommentController extends AbstractController {
	@Autowired
	private CommentService commentService;

	public CommentController() {
		super();
	}

	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public ModelAndView list(@RequestParam int paintingId) {
		ModelAndView result;
		Collection<Comment> comments;

		comments = commentService.findByPainting(paintingId);

		result = new ModelAndView("comment/list");
		result.addObject("requestURI", "comment/list.do");
		result.addObject("comments", comments);
		result.addObject("paintingId", paintingId);
		result.addObject("backURI", "/welcome/index.do");
		return result;
	}

	@RequestMapping(value = "/edit", method = RequestMethod.GET)
	public ModelAndView edit(@RequestParam int commentId) {
		ModelAndView result;
		Comment comment;
		Boolean hasChildren;
		Boolean hasParent;
		hasParent = false;
		Boolean isResponse;

		comment = commentService.findOne(commentId);
		hasChildren = !(comment.getChildren().isEmpty());
		isResponse = false;

		result = new ModelAndView("comment/edit");

		if (comment.getParent() != null) {
			hasParent = true;
			result.addObject("parentCommentId", comment.getParent().getId());

		}
		if (hasChildren) {
			result.addObject("childrenCommentId", commentId);

		}

		result.addObject("requestURI", "comment/edit.do");
		result.addObject("comment", comment);
		result.addObject("hasChildren", hasChildren);
		result.addObject("hasParent", hasParent);
		result.addObject("isResponse", isResponse);
		result.addObject("commentId", commentId);
		result.addObject("backURI", "comment/list.do?paintingId="
				+ comment.getPainting().getId());
		return result;
	}

	@RequestMapping(value = "/children", method = RequestMethod.GET)
	public ModelAndView children(@RequestParam int commentId) {
		ModelAndView result;
		Comment originalComment;
		Collection<Comment> comments;
		int paintingId;

		originalComment = commentService.findOne(commentId);
		comments = originalComment.getChildren();
		paintingId = originalComment.getPainting().getId();

		result = new ModelAndView("comment/list");
		result.addObject("requestURI", "comment/list.do");
		result.addObject("comments", comments);
		result.addObject("paintingId", paintingId);
		result.addObject("commentId", commentId);
		result.addObject("parentCommentId", commentId);

		result.addObject("backURI", "comment/list.do?paintingId=" + paintingId);
		return result;
	}
}
